<?php

	ini_set('display_errors', 'on');
	error_reporting(E_ALL);

	define('DS', DIRECTORY_SEPARATOR);

	// Start debug timer

	$timer_start	= array_sum(explode(' ', microtime()));

	// include configuration file
	include_once 'includes'.DS.'configuration.php';

	include_once _BASE_PATH.DS.'includes'.DS.'classes'.DS.'class.framework.php';
	include_once _BASE_PATH.DS.'includes'.DS.'classes'.DS.'class.security.php';

	include_once _BASE_PATH.DS.'includes'.DS.'smarty'.DS.'Smarty.class.php';

	$smarty		= &new smarty;

	$framework	= &new framework($db_host, $db_user, $db_pass, $db_name, $db_prefix);
	$security	= &new security;
	unset($db_host, $db_user, $db_pass, $db_name);

	// Set Smarty Paths
	$smarty->template_dir = _BASE_PATH.DS.'templates'.DS.$framework->config['smarty_template'].DS;
	$smarty->plugins_dir[] =_BASE_PATH.DS.'includes'.DS.'classes'.DS.'smarty_plugins'.DS;

	$framework->security		= &$security;
	$security->framework		= &$framework;
	$security->session_length	= 10;

	$security->get_authentication();

	$security->check_auto_login();

	$what	= @$_REQUEST['what'];

	if($what == 'export') {
		// the public class is available in both authentication states
		include_once 'includes'.DS.'classes'.DS.'source'.DS.'class.export.php';
	} elseif($security->auth === false) {

		if($what == 'list') {
			// They are trying to call the popup without logging in. STOP EXECUTING
			exit;
		}

		// This user is currently not logged in
		switch($what) {
			case 'public':
				// Functions accessable to the public such as viewing a members collection/wishlist, etc

				break;

			default:
				// Functions for logging in, registering a public account, retrieving passwords, etc
				include_once 'includes/classes/source/class.login.php';
				break;
		}
	} else {
		switch($what) {

			case 'borrow':
				// processing functions for borrowing movies, etc
				include_once 'includes/classes/source/class.borrow.php';
				break;

			case 'public':
				// Functions accessable to the public such as viewing a members collection/wishlist, etc
				include_once 'includes/classes/source/class.public.php';
				break;

			case 'prefrences':
				// Allow the current member to update their details & prefrences
				include_once 'includes/classes/source/class.prefrences.php';
				break;

			case 'members':
				include_once 'includes/classes/source/class.members.php';
				break;

			case 'list':
				include_once 'includes/classes/source/class.list.php';
				break;

			case 'stats':
			case 'statistics':
				include_once 'includes/classes/source/class.stats.php';
				break;

			case 'help':
				include_once 'includes/classes/source/class.help.php';
				break;

			case 'config':
				include_once 'includes/classes/source/class.config.php';
				break;

			case 'invite':
				include_once 'includes/classes/source/class.invite.php';
				break;

			case 'wishlist':
				include_once 'includes/classes/source/class.wishlist.php';
				break;

			case 'search':
				include_once 'includes/classes/source/class.search.php';
				break;

			case 'movie':
				include_once 'includes/classes/source/class.movie.php';
				break;

			case 'mymovies':
			case 'my_movies':
				include_once 'includes/classes/source/class.mymovies.php';
				break;

			default:
				include_once('includes/classes/source/class.welcome.php');
				break;

		}
	}

	if(class_exists('source')) {
		$source = new source(&$framework, &$security);
	}

	// Handle Feedback Messages
	$framework->content['msg'] = isset($_SESSION['msg']) ? $_SESSION['msg'] : false;
	unset($_SESSION['msg']);

	// Assign Smarty Variables
	$smarty->assign('config', $framework->config);

	$smarty->assign('authenticated', $security->auth);
	$smarty->assign('template', $framework->template);
	$smarty->assign('content', $framework->content);

	$smarty->assign('genres', $framework->get_genres());

	$smarty->assign('pc', $framework->pc_build());
	$smarty->assign('js', $framework->js);

	// Assign Stats
	if($security->auth !== false)
	{

		$smarty->assign('stats', $framework->get_welcome_stats());
		$smarty->assign('member', $security->dtls);

	}

	$smarty->assign('random', $framework->get_movie_random(true));

	// Show the template
	if(!empty($framework->main_template)) {
		$smarty->display($framework->main_template);

		$timer_end		= array_sum(explode(' ', microtime()));

		$total_time	= round($timer_end - $timer_start, 9);

		//echo '<center>Page generated in '. $total_time .' seconds.</center>';
	}

?>